
TOOL.Category		= "PC Mod - Wiring"
TOOL.Name			= "#Router Linker"
TOOL.Tab			= "PC Mod"
TOOL.Command		= nil
TOOL.ConfigName		= ""

cleanup.Register( "computers" )

if (CLIENT) then

	language.Add( 'Tool_routerlinker_name', 'Router Linker' )
	language.Add( 'Tool_routerlinker_desc', 'Connects routers to other routers' )
	language.Add( 'Tool_routerlinker_0', 'Left-Click: Select the first router to connect. ' )
	language.Add( 'Tool_routerlinker_1', 'Left-Click: Select the second router to connect to. ' )

end

function TOOL:LeftClick( trace )

	// If the target is a player, stop here.
	if ( trace.Entity && trace.Entity:IsPlayer() ) then return false end
	
	// If there is no physics object, stop here.
	if ( SERVER && !util.IsValidPhysicsObject( trace.Entity, trace.PhysicsBone ) ) then return false end
	
	// Clients can stop here
	local iNum = self:NumObjects()
	if ( CLIENT ) then
		if ( iNum > 0 ) then self:ClearObjects() end
		return true
	end
	
	// Get the player
	local ply = self:GetOwner()
	
	// Get our object
	local Phys = trace.Entity:GetPhysicsObjectNum( trace.PhysicsBone )
	local id = trace.Entity:EntIndex()
	if (iNum == 0) then
		if ((!trace.Entity.IsRouter) || (trace.Entity.IsRepeater)) then
			ply:PrintMessage( HUD_PRINTTALK, "That entity is not a router!" )
			return false
		end
	end
	if (iNum == 1) then
		if (!trace.Entity.IsRouter) then
			ply:PrintMessage( HUD_PRINTTALK, "That entity is not a router!" )
			return false
		end
		local EntA,  EntB  = self:GetEnt(1),	 trace.Entity
		if (EntB.IsRepeater) then
			if (!EntB:HasSpace()) then
				ply:PrintMessage( HUD_PRINTTALK, "The repeater has no spaces left!" )
				return false
			end
		else
			if (EntA:HasRouter( EntB )) && (EntB:HasRouter( EntA )) then
				ply:PrintMessage( HUD_PRINTTALK, "These routers are already linked!" )
				return false
			end
		end
	end
	self:SetObject( iNum + 1, trace.Entity, trace.HitPos, Phys, trace.PhysicsBone, trace.HitNormal )

	if ( iNum > 0 ) then

		// Setup our variables
		local forcelimit = 0
		local addlength	 = 100
		local material 	 = "cable/cable2"
		local width 	 = 2
		local rigid	 	= false
		
		// Get information we're about to use
		local Ent1,  Ent2  = self:GetEnt(1),	 self:GetEnt(2)
		local Bone1, Bone2 = self:GetBone(1),	 self:GetBone(2)
		local WPos1, WPos2 = self:GetPos(1),	 self:GetPos(2)
		local LPos1, LPos2 = self:GetLocalPos(1),self:GetLocalPos(2)
		local length = ( WPos1 - WPos2):Length()

		// Create the rope
		local constraint, rope = constraint.Rope( Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, length+addlength, 0, forcelimit, width, material, rigid )
		
		// Tell both routers to link to each other
		Ent1:RegisterRouter( Ent2 )
		if (Ent2.IsRepeater) then
			Ent2:FillSlot( Ent2:GetEmptySlot(), Ent1 )
		else
			Ent2:RegisterRouter( Ent1 )
		end
		
		// Clear the objects so we're ready to go again
		self:ClearObjects()
	else
		// Advance our stage
		self:SetStage( iNum+1 )
	end
	
	// This was successful!
	return true
end

function TOOL:RightClick()
	return false
end

function TOOL.BuildCPanel( CPanel )

	pc_maketoolinfo( CPanel:AddControl( "Label", { Text = "", Description = "" } ), "Router Linker", "Allows you to link two routers together." )

	// HEADER
	CPanel:AddControl( "Header", { Text = "#Tool_pc_name", Description	= "#Tool_pc_desc" }  )

end